package io.gitee.twoke.generator.engine.impl.persistence.entity.data.impl;

import io.gitee.twoke.generator.engine.impl.cfg.GenerateEngineConfigurationImpl;
import io.gitee.twoke.generator.engine.impl.persistence.TemplateGroupDataManager;
import io.gitee.twoke.generator.engine.impl.persistence.entity.AbstractDataManager;
import io.gitee.twoke.generator.engine.impl.persistence.entity.TemplateGroupEntity;
import io.gitee.twoke.generator.engine.impl.persistence.entity.TemplateGroupEntityImpl;

import java.util.HashMap;
import java.util.List;

public class MybatisTemplateGroupDataManager
    extends AbstractDataManager<TemplateGroupEntity> implements TemplateGroupDataManager {

    public MybatisTemplateGroupDataManager(GenerateEngineConfigurationImpl generateEngineConfiguration) {
        super(generateEngineConfiguration);
    }


    @Override
    public Class<? extends TemplateGroupEntity> getManagedEntityClass() {
        return  TemplateGroupEntityImpl.class;
    }

    @Override
    public TemplateGroupEntity create() {
        return new TemplateGroupEntityImpl();
    }

    @Override
    public void saveTemplateGroupRelation(List<String> templateIds, String groupId) {
        getDbSqlSession().getSqlSession().insert("insertTemplateGroupRelations", new HashMap<String, Object>(){{
            put("templateIds", templateIds);
            put("groupId", groupId);
        }});
    }

    @Override
    public void deleteTemplateRelationByGroupId(String groupId) {
        getDbSqlSession().getSqlSession().delete("deleteTemplateRelationByGroupId",  new HashMap<String, Object>(){{
            put("groupId", groupId);
        }});
    }

    @Override
    public void unbind(String groupId, String templateId) {
        getDbSqlSession().getSqlSession().delete("deleteTemplateGroupRelation",  new HashMap<String, Object>(){{
            put("templateId", templateId);
            put("groupId", groupId);
        }});
    }

    @Override
    public void bind(String groupId, String templateId) {
        getDbSqlSession().getSqlSession().delete("insertTemplateGroupRelation", new HashMap<String, Object>(){{
            put("templateId", templateId);
            put("groupId", groupId);
        }});
    }
}
